package vip.zhenzicheng.rabbitmq.workqueue;

import com.rabbitmq.client.*;
import vip.zhenzicheng.rabbitmq.utils.RabbitConstant;
import vip.zhenzicheng.rabbitmq.utils.RabbitUtils;

import java.io.IOException;

/**
 * @author zhenzicheng
 * @DESCRIPTION: 短信发送服务2 (消费者)
 * @DATE: 2022/04/24 3:18 PM
 */
public class SMSSender2 {
  public static void main(String[] args) throws IOException {
    Connection conn = RabbitUtils.getConnection();
    final Channel channel = conn.createChannel();

    channel.queueDeclare(RabbitConstant.QUEUE_SMS, false, false, false, null);
    channel.basicQos(1);

    channel.basicConsume(RabbitConstant.QUEUE_SMS, false, new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        String jsonSMS = new String(body);
        System.out.println("SMSSender2-短信发送成功" + jsonSMS);
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }

        channel.basicAck(envelope.getDeliveryTag(), false);
      }
    });

  }
}
